﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Configuration;
using System.Threading;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace PowerPlant
{
    public partial class Main : Form
    {
        //应用程序初始化
        public Main()
        {
            InitializeComponent();

            ofd_importFile.FileName = null;
            sfd_exportFile.FileName = null;
            //防止窗口的Tabcontrol 闪动
            this.DoubleBuffered = true;
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
            this.UpdateStyles();

            #region 初始化部分下拉框
            BindComboBoxItem(cb_danjiType, "danjitype");
            BindComboBoxItem(cb_danjiType2, "danjitype");
            BindComboBoxItem(cb_fsgl_dianchang, "dianchang");
            BindComboBoxItem(cb_manage_dianchang1, "dianchang");
            BindComboBoxItem(cb_manage_dianchang2, "dianchang");
            BindComboBoxItem(cb_import_dianchang, "dianchang");
            BindComboBoxItem(cb_adddanji_dianchang, "dianchang");
            cb_danjiType.SelectedItem = cb_danjiType.Items[0];
            cb_danjiType2.SelectedItem = cb_danjiType2.Items[0];
            cb_fsgl_dianchang.SelectedItem = cb_fsgl_dianchang.Items[0];
            cb_manage_dianchang1.SelectedItem = cb_manage_dianchang1.Items[0];
            cb_manage_dianchang2.SelectedItem = cb_manage_dianchang2.Items[0];
            cb_import_dianchang.SelectedItem = cb_import_dianchang.Items[0];
            cb_adddanji_dianchang.SelectedItem = cb_adddanji_dianchang.Items[0];
            cb_fsgl_dianchang_SelectedIndexChanged(null,null);
            #endregion
        }

        #region Internal Method 辅助方法
        //绑定下拉框选项
        private void BindComboBoxItem(ComboBox obj,string tableName)
        {
            obj.Items.Clear();
            //实例化实体数据模型
            PowerPlantEntities PPE = new PowerPlantEntities();
            //在数据库中就读取数据冰绑定到下拉框中
            if(tableName=="danjitype")
            {
                var typeList = PPE.danjitype.ToList();
                foreach (danjitype item in typeList)
                {
                    ComboBoxModel cbm = new ComboBoxModel();
                    cbm.Text = item.danjitype_name.ToString();
                    cbm.Value = item.danjitype_name.ToString();
                    obj.Items.Add(cbm);
                }
            }
            else if(tableName=="dianchang")
            {
                var dianchangList=PPE.dianchang.ToList();
                foreach (dianchang item in dianchangList)
                {
                    ComboBoxModel cbm = new ComboBoxModel();
                    cbm.Text = item.dianchang_name.ToString();
                    cbm.Value = item.dianchang_name.ToString();
                    obj.Items.Add(cbm);
                }
            }
            
        }
        //全选Datagridview
        private void dgv_selectAll(DataGridView dgv)
        {
            foreach (DataGridViewRow dr in dgv.Rows)
            {
                if (dr.Index != dgv.Rows.Count - 1)
                {
                    ((DataGridViewCheckBoxCell)dr.Cells[0]).Value = true;
                }
            }
        }
        //取消全选Datagridview
        private void dgv_unselectAll(DataGridView dgv)
        {
            foreach (DataGridViewRow dr in dgv.Rows)
            {
                if (dr.Index != dgv.Rows.Count - 1)
                {
                    ((DataGridViewCheckBoxCell)dr.Cells[0]).Value = false;
                }
            }
        }
        //设置datagridview只读
        private void SetDatagridviewReadOnly(DataGridView dgv)
        {
            for (int i = 1; i < dgv.Columns.Count; i++)
            {
                dgv.Columns[i].ReadOnly = true;
            }
        }
        //删除电厂
        private void DeleteDianchang(int dianchangId)
        {
            //1.实例化实体数据模型
            //2.删除对应电厂的机组统计数据
            //3.删除对应电厂的机组
            //4.删除对应电厂
            PowerPlantEntities PPE = new PowerPlantEntities();
            var dianchang = PPE.dianchang.First(c => c.dianchang_id == dianchangId);
            string temp=dianchangId.ToString();
            var danjiList = PPE.danji.Where(c => c.danji_dianchangId == temp).ToList();
            var statusList = PPE.status_danji.Where(c => danjiList.Exists(d => d.danji_id.ToString() == c.status_danjiId)).ToList();
            foreach (status_danji item in statusList)
            {
                PPE.status_danji.DeleteObject(item);
            }
            foreach (danji item in danjiList)
            {
                PPE.danji.DeleteObject(item);
            }
            PPE.dianchang.DeleteObject(dianchang);
            PPE.SaveChanges();
        }
        //删除机组
        private void DeleteDanji(int danjiId)
        {
            //1.实例化实体数据模型
            //2.删除对应机组的统计数据
            //3.删除对应电厂的机组
            PowerPlantEntities PPE = new PowerPlantEntities();
            var danji = PPE.danji.First(c => c.danji_id == danjiId);
            string temp=danjiId.ToString();
            var statusList = PPE.status_danji.Where(c => c.status_danjiId == temp).ToList();
            foreach (status_danji item in statusList)
            {
                PPE.status_danji.DeleteObject(item);
            }
            PPE.danji.DeleteObject(danji);
            PPE.SaveChanges();
        }
        //删除数据
        private void DeleteStatus(int statusId)
        {
            //1.实例化实体数据模型
            //2.删除对应的机组统计数据
            PowerPlantEntities PPE = new PowerPlantEntities();
            var status=PPE.status_danji.First(c=>c.status_id==statusId);
            PPE.status_danji.DeleteObject(status);
            PPE.SaveChanges();
        }
        //删除坏数
        private void DeleteBadNum(List<int> idList)
        {
            //1.实例化实体数据模型
            //2.删除对应的坏数
            PowerPlantEntities PPE = new PowerPlantEntities();
            foreach (int id in idList)
            {
                var model = PPE.status_danji.FirstOrDefault(c => c.status_id == id);
                if (model != null)
                {
                    PPE.status_danji.DeleteObject(model);
                }
            }
            PPE.SaveChanges();
        }
        //绘制风速概率分布
        public void DrawChart_danji_fsgl()
        {
            tabControl3.Enabled = false;
            label23.Text = "正在绘制，请稍等...";
            label23.Refresh();
            DataTable result = new DataTable();
            PowerPlantEntities PPE = new PowerPlantEntities();
            DateTime startDate = dtp_fsgl_start.Value;
            DateTime endDate = dtp_fsgl_end.Value;
            string danjiName= cb_fsgl_danji.SelectedItem.ToString();
            string danjiId=PPE.danji.First(c=>c.danji_name==danjiName ).danji_id.ToString();
            List<status_danji> list = PPE.status_danji.Where(c => c.status_danjiId == danjiId).ToList();
            List<status_danji> list1 = list.Where(c => DateTime.Compare(DateTime.Parse((c.status_date).Split(' ')[0]), DateTime.Parse(endDate.ToString().Split(' ')[0])) <= 0 && DateTime.Compare(DateTime.Parse((c.status_date).Split(' ')[0]), DateTime.Parse(startDate.ToString().Split(' ')[0])) >= 0).ToList();
            int sum = list1.Count;
            double xStart =Convert.ToDouble( getConfigurationSetting(ConfigurationList.danjiFengsuMin));
            double xEnd = Convert.ToDouble(getConfigurationSetting(ConfigurationList.danjiFengsuMax));
            double xInterval = trackBar1.Value * Convert.ToDouble(getConfigurationSetting(ConfigurationList.fsglScroll));
            int xCount = Convert.ToInt32((xEnd - xStart) / xInterval);
            double xCursor = xStart;
            result.Columns.Add("x");
            result.Columns.Add("y");
            for (int i = 1; i <= xCount; i++)
            {
                DataRow newRow = result.NewRow();
                newRow[0] = xCursor.ToString()+"~"+(xCursor+xInterval).ToString();
                List<status_danji> list2 = list1.Where(c => Convert.ToDouble(c.status_fengsu) >= xCursor && Convert.ToDouble(c.status_fengsu)<(xCursor+xInterval)).ToList();
                int temp = list2.Count;
                double rate;
                if (sum != 0)
                {
                    rate = temp / Convert.ToDouble( sum);
                }
                else
                {
                    rate = 0;
                }
                newRow[1] = rate;
                result.Rows.Add(newRow);
                xCursor = Math.Round(xCursor + xInterval, 2);
            }
            DrawChart(result, chart1);
            tabControl3.Enabled = true;
            label23.Text ="已完成绘制";
        }
        //绘制出力概率分布
        public void DrawChart_danji_clgl()
        {
            label23.Text = "正在绘制，请稍等...";
            label23.Refresh();
            tabControl3.Enabled = false;
            DataTable result = new DataTable();
            PowerPlantEntities PPE = new PowerPlantEntities();
            DateTime startDate = dtp_fsgl_start.Value;
            DateTime endDate = dtp_fsgl_end.Value;
            string danjiName = cb_fsgl_danji.SelectedItem.ToString();
            string danjiId = PPE.danji.First(c => c.danji_name == danjiName).danji_id.ToString();
            List<status_danji> list = PPE.status_danji.Where(c => c.status_danjiId == danjiId).ToList();
            List<status_danji> list1 = list.Where(c => DateTime.Compare(DateTime.Parse((c.status_date).Split(' ')[0]), DateTime.Parse(endDate.ToString().Split(' ')[0])) <= 0 && DateTime.Compare(DateTime.Parse((c.status_date).Split(' ')[0]), DateTime.Parse(startDate.ToString().Split(' ')[0])) >= 0).ToList();
            int sum = list1.Count;
            double xStart = Convert.ToDouble(getConfigurationSetting(ConfigurationList.danjiChuliMin));
            double xEnd = Convert.ToDouble(getConfigurationSetting(ConfigurationList.danjiChuliMax));
            double xInterval = trackBar2.Value *Convert.ToDouble(getConfigurationSetting(ConfigurationList.clglScroll));
            int xCount =Convert.ToInt32( Math.Ceiling((xEnd - xStart) / xInterval));
            double xCursor = xStart;
            result.Columns.Add("x");
            result.Columns.Add("y");
            for (int i = 1; i <= xCount; i++)
            {
                DataRow newRow = result.NewRow();
                newRow[0] = xCursor.ToString() + "~" + (xCursor + xInterval).ToString();
                List<status_danji> list2 = list1.Where(c => Convert.ToDouble(c.status_power) >= xCursor && Convert.ToDouble(c.status_power) < (xCursor + xInterval)).ToList();
                int temp = list2.Count;
                double rate;
                if (sum != 0)
                {
                    rate = temp / Convert.ToDouble(sum);
                }
                else
                {
                    rate = 0;
                }
                newRow[1] = rate;
                result.Rows.Add(newRow);
                xCursor = Math.Round(xCursor + xInterval, 2);
            }
            DrawChart(result, chart2);
            tabControl3.Enabled = true;
            label23.Text = "已完成绘制";
        }
        //绘制风速曲线
        public void DrawChart_danji_fsqx()
        {
            label23.Text = "正在绘制，请稍等...";
            label23.Refresh();
            tabControl3.Enabled = false;
            DataTable result = new DataTable();
            PowerPlantEntities PPE = new PowerPlantEntities();
            DateTime startDate =DateTime.Parse( dtp_fsgl_start.Value.ToString().Split(' ')[0]);
            DateTime endDate = DateTime.Parse(dtp_fsgl_end.Value.ToString().Split(' ')[0]);
            string danjiName = cb_fsgl_danji.SelectedItem.ToString();
            string danjiId = PPE.danji.First(c => c.danji_name == danjiName).danji_id.ToString();
            List<status_danji> list = PPE.status_danji.Where(c => c.status_danjiId == danjiId).ToList();
            List<status_danji> list1 = list.Where(c => DateTime.Compare(DateTime.Parse((c.status_date).Split(' ')[0]), endDate) <= 0 && DateTime.Compare(DateTime.Parse((c.status_date).Split(' ')[0]), startDate) >= 0).ToList();
            TimeSpan ts=new TimeSpan();
            ts = endDate.AddDays(1) - startDate;
            double xInterval = trackBar3.Value * Convert.ToDouble(getConfigurationSetting(ConfigurationList.fsqxScroll));
            int xCount = Convert.ToInt32(Math.Ceiling((ts.TotalSeconds) / xInterval));
            DateTime xCursor = startDate;
            result.Columns.Add("x");
            result.Columns.Add("y");
            for (int i = 1; i <= xCount; i++)
            {
                DataRow newRow = result.NewRow();
                newRow[0] = xCursor.ToString();
                var model = list1.FirstOrDefault(c => DateTime.Compare(DateTime.Parse(c.status_date), xCursor) >= 0 && DateTime.Compare(DateTime.Parse(c.status_date), xCursor.AddSeconds(xInterval)) <= 0);
                if (model != null)
                {
                    newRow[1] = model.status_fengsu;
                }
                else
                {
                    newRow[1] = 0;
                }
                result.Rows.Add(newRow);
                xCursor = xCursor.AddSeconds(xInterval);
            }
            DrawChart(result, chart3);
            tabControl3.Enabled = true;
            label23.Text = "已完成绘制";
        }
        //绘制出力曲线
        public void DrawChart_danji_clqx()
        {
            label23.Text = "正在绘制，请稍等...";
            label23.Refresh();
            tabControl3.Enabled = false;
            DataTable result = new DataTable();
            PowerPlantEntities PPE = new PowerPlantEntities();
            DateTime startDate = DateTime.Parse(dtp_fsgl_start.Value.ToString().Split(' ')[0]);
            DateTime endDate = DateTime.Parse(dtp_fsgl_end.Value.ToString().Split(' ')[0]);
            string danjiName = cb_fsgl_danji.SelectedItem.ToString();
            string danjiId = PPE.danji.First(c => c.danji_name == danjiName).danji_id.ToString();
            List<status_danji> list = PPE.status_danji.Where(c => c.status_danjiId == danjiId).ToList();
            List<status_danji> list1 = list.Where(c => DateTime.Compare(DateTime.Parse(c.status_date.Split(' ')[0]), endDate) <= 0 && DateTime.Compare(DateTime.Parse(c.status_date.Split(' ')[0]), startDate) >= 0).ToList();
            TimeSpan ts = new TimeSpan();
            ts = endDate.AddDays(1) - startDate;
            double xInterval = trackBar4.Value * Convert.ToDouble(getConfigurationSetting(ConfigurationList.clqxScroll));
            int xCount = Convert.ToInt32(Math.Ceiling((ts.TotalSeconds) / xInterval));
            DateTime xCursor = startDate;
            result.Columns.Add("x");
            result.Columns.Add("y");
            for (int i = 1; i <= xCount; i++)
            {
                DataRow newRow = result.NewRow();
                newRow[0] = xCursor.ToString();
                var model = list1.FirstOrDefault(c => DateTime.Compare(DateTime.Parse(c.status_date), xCursor) >= 0 && DateTime.Compare(DateTime.Parse(c.status_date), xCursor.AddSeconds(xInterval)) <= 0);
                if (model != null)
                {
                    newRow[1] = model.status_power;
                }
                else
                {
                    newRow[1] = 0;
                }
                result.Rows.Add(newRow);
                xCursor = xCursor.AddSeconds(xInterval);
            }
            DrawChart(result, chart4);
            tabControl3.Enabled = true;
            label23.Text = "已完成绘制";
        }
        //绘制电厂出力曲线
        public void DrawChart_dianchang_clqx()
        {
            label23.Text = "正在绘制，请稍等...";
            label23.Refresh();
            DataTable result = new DataTable();
            PowerPlantEntities PPE = new PowerPlantEntities();
            DateTime startDate = DateTime.Parse(dtp_fsgl_start.Value.ToString().Split(' ')[0]);
            DateTime endDate = DateTime.Parse(dtp_fsgl_end.Value.ToString().Split(' ')[0]);
            string dianchangName = cb_fsgl_dianchang.SelectedItem.ToString();
            string dianchangId = PPE.dianchang.First(c => c.dianchang_name == dianchangName).dianchang_id.ToString();
            List<string> totalPowerList = new List<string>();
            var danjiList = PPE.danji.Where(c => c.danji_dianchangId == dianchangId).ToList();
            var temp = PPE.status_danji.ToList();
            var list = temp.Where(c => danjiList.Exists(d => d.danji_id.ToString() == c.status_danjiId)).ToList();
            TimeSpan ts = new TimeSpan();
            ts = endDate.AddDays(1) - startDate;
            double xInterval = trackBar5.Value * Convert.ToDouble(getConfigurationSetting(ConfigurationList.clqxScroll));
            int xCount = Convert.ToInt32(Math.Ceiling((ts.TotalSeconds) / xInterval));
            if (list.FirstOrDefault(c => DateTime.Compare(DateTime.Parse(c.status_date), startDate) >= 0) == null)
            {
                tabControl3.Enabled = true;
                label23.Text = "已完成绘制";
                return;
            }
            DateTime xCursor = DateTime.Parse(list.First(c => DateTime.Compare(DateTime.Parse(c.status_date), startDate) >= 0).status_date);
            result.Columns.Add("x");
            result.Columns.Add("y");
            tabControl3.Enabled = false;
            for (int j = 0; j < xCount; j++)
            {
                DataRow row = result.NewRow();
                string datePoint = xCursor.ToString();
                var list1 = list.Where(c =>string.Equals( c.status_date, datePoint.Replace('-','/'))).ToList();
                row[0] = datePoint;
                row[1] = 0;
                foreach (status_danji item in list1)
                {
                    row[1] = Convert.ToDouble(row[1]) + Convert.ToDouble(item.status_power);
                }
                
                xCursor = xCursor.AddSeconds(xInterval);
                result.Rows.Add(row);
            }
            DrawChart(result, chart5);
            tabControl3.Enabled = true;
            label23.Text = "已完成绘制";
        }
        //计算数据汇总
        public void DrawSummary()
        {
            lb_summary_dianchang.Text = cb_fsgl_dianchang.SelectedItem.ToString();
            lb_summary_danji.Text = cb_fsgl_danji.SelectedItem.ToString();
            PowerPlantEntities PPE = new PowerPlantEntities();
            DateTime startDate = DateTime.Parse(dtp_fsgl_start.Value.ToString().Split(' ')[0]);
            DateTime endDate = DateTime.Parse(dtp_fsgl_end.Value.ToString().Split(' ')[0]);
            string dianchangName = cb_fsgl_dianchang.SelectedItem.ToString();
            string dianchangId = PPE.dianchang.First(c => c.dianchang_name == dianchangName).dianchang_id.ToString();
            #region 计算电厂数据
            var danjiList = PPE.danji.Where(c => c.danji_dianchangId == dianchangId).ToList();
            var temp = PPE.status_danji.ToList();
            var list = temp.Where(c => danjiList.Exists(d => d.danji_id.ToString() == c.status_danjiId)).ToList();
            TimeSpan ts = new TimeSpan();
            ts = endDate.AddDays(1) - startDate;
            double xInterval = Convert.ToDouble(getConfigurationSetting(ConfigurationList.statusInterval));
            int xCount = Convert.ToInt32(Math.Ceiling((ts.TotalSeconds) / xInterval));
            DataTable dianchangSource = new DataTable();
            if (list.FirstOrDefault(c => DateTime.Compare(DateTime.Parse(c.status_date), startDate) >= 0) == null) 
            {
                MessageBox.Show("所选时间段中没有数据！！");
                return;
            }

            DateTime xCursor = DateTime.Parse(list.First(c => DateTime.Compare(DateTime.Parse(c.status_date), startDate) >= 0).status_date);
            dianchangSource.Columns.Add("x");
            dianchangSource.Columns.Add("y");
            tabPage13.Refresh();
            tabControl3.Enabled = false;
            for (int j = 0; j < xCount; j++)
            {
                DataRow row = dianchangSource.NewRow();
                string datePoint = xCursor.ToString();
                var list1 = list.Where(c => c.status_date == datePoint.Replace('-', '/')).ToList();
                row[0] = 0;
                foreach (status_danji item in list1)
                {
                    row[0] = Convert.ToDouble(row[0]) + Convert.ToDouble(item.status_power);
                }
                xCursor = xCursor.AddSeconds(xInterval);
                dianchangSource.Rows.Add(row);
                bt_CalSummary.Text = "正在处理数据：" + (Math.Round((j + 1) / Convert.ToDouble(xCount) * 100, 4)).ToString() + "%";
                bt_CalSummary.Refresh();
            }
            string strTempResult = CalAverageFromDT_dianchang(dianchangSource).ToString();
            lb_dianchangStatic_averageChuli.Text = Math.Round(CalAverageFromDT_dianchang(dianchangSource), 6).ToString();
            lb_dianchangStatic_maxChuli.Text = CalMaxFromDT_dianchang(dianchangSource).ToString();
            lb_dianchangStatic_minChuli.Text = CalMinFromDT_dianchang(dianchangSource).ToString();
            #endregion
            #region 计算机组数据

            string danjiName = cb_fsgl_danji.SelectedItem.ToString();
            string danjiId = PPE.danji.First(c => c.danji_name == danjiName).danji_id.ToString();
            var tempStatusList=PPE.status_danji.Where(c=>c.status_danjiId==danjiId).ToList();
            var danjiSourceList = tempStatusList.Where(c => DateTime.Compare(DateTime.Parse(c.status_date.Split(' ')[0]), startDate) >= 0
                                                                                    && DateTime.Compare(DateTime.Parse(c.status_date.Split(' ')[0]), endDate) <= 0).ToList();
            DataTable danjiSource1 = new DataTable();
            danjiSource1.Columns.Add("x");
            danjiSource1.Columns.Add("y");
            DataTable danjiSource2 = new DataTable();
            danjiSource2.Columns.Add("x");
            danjiSource2.Columns.Add("y");
            foreach (status_danji item in danjiSourceList)
            {
                DataRow row1 = danjiSource1.NewRow();
                row1[0] = item.status_fengsu;
                danjiSource1.Rows.Add(row1);
                DataRow row2 = danjiSource2.NewRow();
                row2[0] = item.status_power;
                danjiSource2.Rows.Add(row2);
            }

            lb_danjiStatic_averageChuli.Text = Math.Round( CalAverageFromDT_danji(danjiSource2, 
                Convert.ToDouble(getConfigurationSetting(ConfigurationList.danjiChuliMax)), 
                Convert.ToDouble(getConfigurationSetting(ConfigurationList.danjiChuliMin))),6).ToString();
            lb_danjiStatic_maxChuli.Text = CalMaxFromDT_danji(danjiSource2,
                Convert.ToDouble(getConfigurationSetting(ConfigurationList.danjiChuliMax)),
                Convert.ToDouble(getConfigurationSetting(ConfigurationList.danjiChuliMin))).ToString();
            lb_danjiStatic_minChuli.Text = CalMinFromDT_danji(danjiSource2,
                Convert.ToDouble(getConfigurationSetting(ConfigurationList.danjiChuliMax)),
                Convert.ToDouble(getConfigurationSetting(ConfigurationList.danjiChuliMin))).ToString();
            lb_danjiStatic_badChuli.Text = CalBadNumCountFromDT_danji(danjiSource2,
                Convert.ToDouble(getConfigurationSetting(ConfigurationList.danjiChuliMax)),
                Convert.ToDouble(getConfigurationSetting(ConfigurationList.danjiChuliMin))).ToString();

            lb_danjiStatic_averageFengsu.Text =Math.Round( CalAverageFromDT_danji(danjiSource1,
                Convert.ToDouble(getConfigurationSetting(ConfigurationList.danjiFengsuMax)),
                Convert.ToDouble(getConfigurationSetting(ConfigurationList.danjiFengsuMin))),6).ToString();
            lb_danjiStatic_maxFengsu.Text = CalMaxFromDT_danji(danjiSource1,
                Convert.ToDouble(getConfigurationSetting(ConfigurationList.danjiFengsuMax)),
                Convert.ToDouble(getConfigurationSetting(ConfigurationList.danjiFengsuMin))).ToString();
            lb_danjiStatic_minFengsu.Text = CalMinFromDT_danji(danjiSource1,
                Convert.ToDouble(getConfigurationSetting(ConfigurationList.danjiFengsuMax)),
                Convert.ToDouble(getConfigurationSetting(ConfigurationList.danjiFengsuMin))).ToString();
            lb_danjiStatic_badFengsu.Text = CalBadNumCountFromDT_danji(danjiSource1,
                Convert.ToDouble(getConfigurationSetting(ConfigurationList.danjiFengsuMax)),
                Convert.ToDouble(getConfigurationSetting(ConfigurationList.danjiFengsuMin))).ToString(); ;
            #endregion

            tabControl3.Enabled = true;
        }
        //绘制chart 控件
        private void DrawChart(DataTable result, Chart chart)
        {
            var series1 = chart.Series[0];
            series1.YValueType =ChartValueType.String;
            series1.Points.Clear();
            foreach (DataRow row in result.Rows)
            {
                DataPoint point = new DataPoint();
                point.SetValueXY(row.ItemArray[0], row.ItemArray[1]);
                series1.Points.Add(point);
            }
        }
        //重绘
        private void ReDrawChart()
        {
            switch (tabControl3.SelectedIndex)
            {
                case 0: DrawChart_danji_fsgl(); break;
                case 1: DrawChart_danji_clgl(); break;
                case 2: DrawChart_danji_fsqx(); break;
                case 3: DrawChart_danji_clqx(); break;
                case 4: DrawChart_dianchang_clqx(); break;
                case 5:  break;
                default: return ;
            }
        }
        //更新DataGridView
        private void DrawGridView()
        {
            switch (tabControl2.SelectedIndex)
            {
                case 0: dgv_dianchang.DataSource = DataManager.getDataTableByName();
                    SetDatagridviewReadOnly(dgv_dianchang); break;
                case 1: dgv_danji.DataSource = DataManager.getDataTableByName(cb_manage_dianchang1.SelectedItem.ToString());
                    SetDatagridviewReadOnly(dgv_danji); break;
                case 2: dgv_data.DataSource = DataManager.getDataTableByName(cb_manage_danji.SelectedItem.ToString(), cb_manage_dianchang2.SelectedItem.ToString());
                    SetDatagridviewReadOnly(dgv_data); break;
                default: return;
            }
        }
        //读取配置文件信息
        private string getConfigurationSetting(ConfigurationList key)
        {
            return ConfigurationManager.AppSettings[key.ToString()];
        }
        //设置配置文件
        private void setConfigurationSetting(ConfigurationList key, string value)
        {
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfa.AppSettings.Settings[key.ToString()].Value=value;
            cfa.Save();
            return;
        }
        //加载设置页面
        private void LoadSettingPage()
        {
            tb_setting_chuliMax.Text = getConfigurationSetting(ConfigurationList.danjiChuliMax);
            tb_setting_chuliMin.Text = getConfigurationSetting(ConfigurationList.danjiChuliMin);
            tb_setting_fengsuMax.Text = getConfigurationSetting(ConfigurationList.danjiFengsuMax);
            tb_setting_fengsuMin.Text = getConfigurationSetting(ConfigurationList.danjiFengsuMin);
            tb_setting_clglScroll.Text = getConfigurationSetting(ConfigurationList.clglScroll);
            tb_setting_clqxScroll.Text = getConfigurationSetting(ConfigurationList.clqxScroll);
            tb_setting_fsglScroll.Text = getConfigurationSetting(ConfigurationList.fsglScroll);
            tb_setting_fsqxScroll.Text = getConfigurationSetting(ConfigurationList.fsqxScroll);
            tb_setting_statusInterval.Text = getConfigurationSetting(ConfigurationList.statusInterval);
        }
        //视图将字符串类型转换为Double类型
        private bool TryParseDoubleFromString(string str)
        {
            try
            {
                Convert.ToDouble(str);
                return true;
            }
            catch
            {
                return false;
            }
        }
        //计算电厂平均处理
        private double CalAverageFromDT_dianchang(DataTable source)
        {
            double sum = 0;
            foreach (DataRow item in source.Rows)
            {
                sum +=Convert.ToDouble( item[0]);
            }
            return sum / Convert.ToDouble(source.Rows.Count) ;
        }
        //计算电厂最大出力
        private double CalMaxFromDT_dianchang(DataTable source)
        {
            double _max = 0;
            foreach (DataRow item in source.Rows)
            {
                _max = Math.Max(_max, Convert.ToDouble(item[0]));
            }
            return _max;
        }
        //计算电厂最小出力
        private double CalMinFromDT_dianchang(DataTable source)
        {
            double _min=0 ;
            foreach (DataRow item in source.Rows)
            {
                if (_min == 0)
                {
                    _min = Convert.ToDouble(item[0]);
                }
                if (Convert.ToDouble(item[0]) < _min)
                {
                    _min = Convert.ToDouble(item[0]);
                }
            }
            return _min;
        }
        //计算机组数据平均数
        private double CalAverageFromDT_danji(DataTable source, double max, double min)
        {
            double sum = 0;
            foreach (DataRow item in source.Rows)
            {
                
                if (Convert.ToDouble(item[0]) >= min && Convert.ToDouble(item[0]) <= max)
                {
                    
                    sum += Convert.ToDouble(item[0]);
                }
                else
                {
                    
                }
            }
            return sum / source.Rows.Count;
        }
        //计算机组数据最大数
        private double CalMaxFromDT_danji(DataTable source, double max, double min)
        {
            double _max = 0;
            foreach (DataRow item in source.Rows)
            {
                if (Convert.ToDouble(item[0]) >= min && Convert.ToDouble(item[0]) <= max)
                {
                    if (Convert.ToDouble(item[0]) > _max)
                    {
                        _max = Convert.ToDouble(item[0]);
                    }
                    else
                    {
                        //MessageBox.Show(item.ItemArray[0].ToString());
                    }
                }
            }
            return _max;
        }
        //计算机组数据最小数
        private double CalMinFromDT_danji(DataTable source, double max, double min)
        {
            double _min = max;
            foreach (DataRow item in source.Rows)
            {
                if (Convert.ToDouble(item[0]) >= min && Convert.ToDouble(item[0]) <= max)
                {
                    if (Convert.ToDouble(item[0]) < _min)
                    {
                        _min = Convert.ToDouble(item[0]);
                    }
                }
            }
            if (_min == max)
            {
                _min = 0;
            }
            return _min;
        }
        //计算机组数据坏数
        private int CalBadNumCountFromDT_danji(DataTable source, double max, double min)
        {
            int count = 0;
            foreach (DataRow item in source.Rows)
            {
                //MessageBox.Show(item.ItemArray[0].ToString());
                if (!(Convert.ToDouble(item[0]) >= min && Convert.ToDouble(item[0]) <= max))
                {
                    count += 1;
                }
            }
            return count;
        }
        #endregion

        #region ButtonClickHandler
        private void bt_showImportFileDialog_Click(object sender, EventArgs e)
        {
            ofd_importFile.ShowDialog();
            lb_importFileName.Text = ofd_importFile.FileName;
        }
        private void bt_import_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ofd_importFile.FileName))
            {
                if (MessageBox.Show("确定要导入当前机组的数据？", "导入数据", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    if (ExcelDriveManage.ImportDataFromExcel(ofd_importFile.FileName, cb_import_danji.SelectedItem.ToString()))
                    {
                        ofd_importFile.FileName = null;
                        lb_importFileName.Text = "未选择文件";
                        MessageBox.Show("导入成功！！");
                        DrawGridView();
                    }
                    else
                    {
                        MessageBox.Show("导入失败！！");
                    }
                }
                
            }
            else
            {
                MessageBox.Show("请先选择文件！！");
            }
        }
        private void bt_selectAll_status_Click(object sender, EventArgs e)
        {
            dgv_selectAll(dgv_data);
        }
        private void bt_selectAll_danji_Click(object sender, EventArgs e)
        {
            dgv_selectAll(dgv_danji);
        }
        private void bt_selectAll_dianchang_Click(object sender, EventArgs e)
        {
            dgv_selectAll(dgv_dianchang);
        }
        private void bt_unselectAll_dianchang_Click(object sender, EventArgs e)
        {
            dgv_unselectAll(dgv_dianchang);
        }
        private void bt_unselectAll_danji_Click(object sender, EventArgs e)
        {
            dgv_unselectAll(dgv_danji);
        }
        private void bt_unselectAll_status_Click(object sender, EventArgs e)
        {
            dgv_unselectAll(dgv_data);
        }
        private void bt_addNewdanji_Click(object sender, EventArgs e)
        {
            #region Data Validation
            if (tb_newdianchang_name.Text.Length <= 0)
            {
                MessageBox.Show("请添加风电场的名称！！");
                return;
            }
            if (tb_newdanji_name.Text.Length <= 3)
            {
                MessageBox.Show("请添加规范的机组名称");
                return;
            }
            if (cb_danjiType.SelectedIndex < 0)
            {
                MessageBox.Show("请选择机组类型");
                return;
            }
            if (tb_newdanjiPower.Text.Length <= 0)
            {
                MessageBox.Show("请填写机组容量");
                return;
            }
            try
            {
                Convert.ToInt32(tb_newdanjiPower.Text);
            }
            catch
            {
                MessageBox.Show("请填写合法的机组容量");
                return;
            }
            #endregion

            ListViewItem item = new ListViewItem();
            item.Text = tb_newdanji_name.Text + "------" + ((ComboBoxModel)cb_danjiType.SelectedItem).Value + "------" + tb_newdanjiPower.Text;
            lv_newdianchang_danji.Items.Add(item);
            string strTemp = tb_newdanji_name.Text;
            tb_newdanji_name.Text = strTemp.Substring(0, strTemp.Length - 1) + (Convert.ToInt32(strTemp.Substring(strTemp.Length - 1)) + 1).ToString();
        }
        private void bt_addNewDianchang_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要添加此电厂！！！！", "添加新电厂", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                PowerPlantEntities PPE = new PowerPlantEntities();
                var dianchangModel = new dianchang();
                dianchangModel.dianchang_capacity = "0";
                dianchangModel.dianchang_name = tb_newdianchang_name.Text;
                PPE.AddTodianchang(dianchangModel);
                PPE.SaveChanges();
                int intDianchangId = PPE.dianchang.First(c => c.dianchang_name == tb_newdianchang_name.Text).dianchang_id;
                int intDianchangPower = 0;
                foreach (ListViewItem item in lv_newdianchang_danji.Items)
                {
                    var danjiModel = new danji();
                    string strItem = item.Text;
                    string strDanjiName = strItem.Substring(0, strItem.IndexOf("------"));
                    string strDanjiPower = strItem.Substring(strItem.LastIndexOf("------") + 6);
                    string strDanjiType = strItem.Substring(strItem.IndexOf("------") + 6, strItem.LastIndexOf("------") - strItem.IndexOf("------") - 6);
                    danjiModel.danji_name = strDanjiName;
                    danjiModel.danji_power = strDanjiPower;
                    danjiModel.danji_type = strDanjiType;
                    danjiModel.danji_dianchangId = intDianchangId.ToString();
                    intDianchangPower += Convert.ToInt32(strDanjiPower);
                    PPE.AddTodanji(danjiModel);
                }
                PPE.dianchang.First(c => c.dianchang_name == tb_newdianchang_name.Text).dianchang_capacity = intDianchangPower.ToString();
                PPE.SaveChanges();
                MessageBox.Show("电厂添加成功！！！");
            }
            else
            {

            }
        }
        private void bt_applySetting_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tb_setting_chuliMax.Text)||
                string.IsNullOrEmpty(tb_setting_chuliMin.Text)||
                string.IsNullOrEmpty(tb_setting_fengsuMax.Text)||
                string.IsNullOrEmpty(tb_setting_fengsuMin.Text)||
                string.IsNullOrEmpty(tb_setting_fsglScroll.Text)||
                string.IsNullOrEmpty(tb_setting_clglScroll.Text) ||
                string.IsNullOrEmpty(tb_setting_fsqxScroll.Text) ||
                string.IsNullOrEmpty(tb_setting_clqxScroll.Text)||
                string.IsNullOrEmpty(tb_setting_statusInterval.Text)
                )
            {
                MessageBox.Show("提交信息不全，请检查！！");
                return;
            }
            if (!TryParseDoubleFromString(tb_setting_chuliMax.Text) ||
                !TryParseDoubleFromString(tb_setting_chuliMin.Text) ||
                !TryParseDoubleFromString(tb_setting_fengsuMax.Text) ||
                !TryParseDoubleFromString(tb_setting_fengsuMin.Text) ||
                !TryParseDoubleFromString(tb_setting_fsglScroll.Text) ||
                !TryParseDoubleFromString(tb_setting_clglScroll.Text) ||
                !TryParseDoubleFromString(tb_setting_fsqxScroll.Text) ||
                !TryParseDoubleFromString(tb_setting_clqxScroll.Text)||
                !TryParseDoubleFromString(tb_setting_statusInterval.Text))
            {
                MessageBox.Show("提交的字段有非法字符！！（提示：请在文本框中输入浮点型或整型数据）");
                return;
            }
            if (MessageBox.Show("确定要更改配置？","更改配置", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                try
                {
                    setConfigurationSetting(ConfigurationList.danjiChuliMax, tb_setting_chuliMax.Text);
                    setConfigurationSetting(ConfigurationList.danjiChuliMin, tb_setting_chuliMin.Text);
                    setConfigurationSetting(ConfigurationList.danjiFengsuMax, tb_setting_fengsuMax.Text);
                    setConfigurationSetting(ConfigurationList.danjiFengsuMin, tb_setting_fengsuMin.Text);
                    setConfigurationSetting(ConfigurationList.fsglScroll, tb_setting_fsglScroll.Text);
                    setConfigurationSetting(ConfigurationList.fsqxScroll, tb_setting_fsqxScroll.Text);
                    setConfigurationSetting(ConfigurationList.clglScroll, tb_setting_clglScroll.Text);
                    setConfigurationSetting(ConfigurationList.clqxScroll, tb_setting_clqxScroll.Text);
                    setConfigurationSetting(ConfigurationList.statusInterval, tb_setting_statusInterval.Text);
                    MessageBox.Show("配置更改成成功，将在重新启动程序后有效。");
                }
                catch(Exception ex)
                {
                    MessageBox.Show("配置更新失败，错误信息："+ex.Message);
                }
            }
            
        }
        private void bt_deleteSelected_dianchang_Click(object sender, EventArgs e)
        {
            List<string> dianchangList = new List<string>();
            foreach (DataGridViewRow item in dgv_dianchang.Rows)
            {
                if (((DataGridViewCheckBoxCell)item.Cells[0]).Value != null && Convert.ToBoolean(((DataGridViewCheckBoxCell)item.Cells[0]).Value.ToString().ToLower()) == true)
                {
                    var temp = ((DataGridViewTextBoxCell)item.Cells[1]).Value;
                    if (temp != null && !string.IsNullOrEmpty(temp.ToString()))
                    {
                        dianchangList.Add(temp.ToString());
                    }
                }
            }
            if (dianchangList.Count <= 0)
            {
                MessageBox.Show("为选择任何列！！");
                return;
            }
            if (MessageBox.Show("确定要删除所选电厂及相关数据？", "删除电厂", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {

                //Delete dianchang Here
                foreach (string item in dianchangList)
                {
                    DeleteDianchang(Convert.ToInt32(item));
                }
                MessageBox.Show("所选电厂及相关数据删除成功！！");
                DrawGridView();
            }
        }
        private void bt_deleteSelected_danji_Click(object sender, EventArgs e)
        {
            List<string> danjiList = new List<string>();
            foreach (DataGridViewRow item in dgv_danji.Rows)
            {
                if (((DataGridViewCheckBoxCell)item.Cells[0]).Value != null && Convert.ToBoolean(((DataGridViewCheckBoxCell)item.Cells[0]).Value.ToString().ToLower()) == true)
                {
                    var temp = ((DataGridViewTextBoxCell)item.Cells[1]).Value;
                    if (temp != null && !string.IsNullOrEmpty(temp.ToString()))
                    {
                        danjiList.Add(temp.ToString());
                    }
                }
            }
            if (danjiList.Count <= 0)
            {
                MessageBox.Show("为选择任何列！！");
                return;
            }
            if (MessageBox.Show("确定要删除所选机组及相关数据？", "删除机组", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                //Delete danji Here
                foreach (string item in danjiList)
                {
                    DeleteDanji(Convert.ToInt32(item));
                }
                MessageBox.Show("所选电厂及相关数据删除成功！！");
                DrawGridView();
            }
        }
        private void bt_deleteSelected_status_Click(object sender, EventArgs e)
        {
            List<string> statusList = new List<string>();
            foreach (DataGridViewRow item in dgv_danji.Rows)
            {
                if (((DataGridViewCheckBoxCell)item.Cells[0]).Value != null && Convert.ToBoolean(((DataGridViewCheckBoxCell)item.Cells[0]).Value.ToString().ToLower()) == true)
                {
                    var temp = ((DataGridViewTextBoxCell)item.Cells[1]).Value;
                    if (temp != null && !string.IsNullOrEmpty(temp.ToString()))
                    {
                        statusList.Add(temp.ToString());
                    }
                }
            }
            if (statusList.Count <= 0)
            {
                MessageBox.Show("为选择任何列！！");
                return;
            }
            if (MessageBox.Show("确定要删除所选数据？", "删除数据", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                //Delete danji Here
                foreach (string item in statusList)
                {
                    DeleteStatus(Convert.ToInt32(item));
                }
                MessageBox.Show("所选数据删除成功！！");
                DrawGridView();
            }
        }
        private void bt_exportSelected_dianchang_Click(object sender, EventArgs e)
        {

        }
        private void bt_exportSelected_danji_Click(object sender, EventArgs e)
        {

        }
        private void bt_exportSelected_status_Click(object sender, EventArgs e)
        {
            sfd_exportFile.Filter = "导出Excel (*.xls)|*.xls";
            sfd_exportFile.FilterIndex = 0;
            sfd_exportFile.RestoreDirectory = true;
            sfd_exportFile.CreatePrompt = true;
            sfd_exportFile.Title = "导出文件保存路径";
            sfd_exportFile.ShowDialog();
            if (!string.IsNullOrEmpty(sfd_exportFile.FileName))
            {
                ExcelDriveManage.ExportExcel(dgv_data, sfd_exportFile.FileName);
                sfd_exportFile.FileName = "";
                MessageBox.Show("导出成功！！");
            }

        }
        private void bt_removeBadNum_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要删除当前机组的坏数？提示：坏数删除后将无法恢复", "删除坏数", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                double maxChuli = Convert.ToDouble(getConfigurationSetting(ConfigurationList.danjiChuliMax));
                double minChuli = Convert.ToDouble(getConfigurationSetting(ConfigurationList.danjiChuliMin));
                double maxFengsu = Convert.ToDouble(getConfigurationSetting(ConfigurationList.danjiFengsuMax));
                double minFengsu = Convert.ToDouble(getConfigurationSetting(ConfigurationList.danjiFengsuMin));
                int totalBadNum = 0;
                int chuliBadNum = 0;
                int fengsuBadNum = 0;
                int bothBadNum = 0;
                List<int> errorList = new List<int>();
                foreach (DataGridViewRow item in dgv_data.Rows)
                {
                    if (item.Index != dgv_data.Rows.Count - 1)
                    {
                        int id = Convert.ToInt32(((DataGridViewTextBoxCell)item.Cells[1]).Value);
                        double _fengsu = Convert.ToDouble(((DataGridViewTextBoxCell)item.Cells[2]).Value);
                        double _chuli = Convert.ToDouble(((DataGridViewTextBoxCell)item.Cells[3]).Value);
                        if ((_fengsu >= maxFengsu || _fengsu <= minFengsu) && (_chuli >= maxChuli || _chuli <= minChuli))
                        {
                            bothBadNum++;
                            totalBadNum++;
                            errorList.Add(id);
                        }
                        else if (_fengsu >= maxFengsu || _fengsu <= minFengsu)
                        {
                            fengsuBadNum++;
                            totalBadNum++;
                            errorList.Add(id);
                        }
                        else if (_chuli >= maxChuli || _chuli <= minChuli)
                        {
                            chuliBadNum++;
                            totalBadNum++;
                            errorList.Add(id);
                        }
                    }

                }
                if (MessageBox.Show("目前坏数统计：\n 风速坏数：" + (fengsuBadNum + bothBadNum).ToString()
                                                                   + "\n出力坏数：" + (fengsuBadNum + bothBadNum).ToString()
                                                                   + "\n总坏数：" + totalBadNum.ToString()
                                                                   + "\n确定要删除这些坏数？",
                    "统计结果", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    DeleteBadNum(errorList);
                    MessageBox.Show("坏数删除成功！");
                    DrawGridView();
                }
            }
        }
        private void bt_addNewDanji2_Click(object sender, EventArgs e)
        {
            PowerPlantEntities PPE = new PowerPlantEntities();
            string dianchangName = cb_adddanji_dianchang.SelectedItem.ToString();
            var dianchangId = PPE.dianchang.FirstOrDefault(c => c.dianchang_name == dianchangName).dianchang_id;
            string tempId = dianchangId.ToString();
            var danjiList = PPE.danji.Where(c => c.danji_dianchangId == tempId);
            #region Data Validation
            if (tb_newdanji_name2.Text.Length <= 3)
            {
                MessageBox.Show("请添加规范的机组名称");
                return;
            }
            var modelList = danjiList.Where(c => c.danji_name == tb_newdanji_name2.Text).ToList();
            if (modelList.Count != 0)
            {
                MessageBox.Show("所要添加的机组名称已存在，请按规则更换名称！");
                return;
            }
            if (cb_danjiType2.SelectedIndex < 0)
            {
                MessageBox.Show("请选择机组类型");
                return;
            }
            if (tb_newdanjiPower2.Text.Length <= 0)
            {
                MessageBox.Show("请填写机组容量");
                return;
            }
            try
            {
                Convert.ToInt32(tb_newdanjiPower2.Text);
            }
            catch
            {
                MessageBox.Show("请填写合法的机组容量");
                return;
            }
            #endregion
            var dianchangModel = PPE.dianchang.First(c => c.dianchang_name == dianchangName);
            int intDianchangId = dianchangModel.dianchang_id;

            var danjiModel = new danji();
            danjiModel.danji_name = tb_newdanji_name2.Text;
            danjiModel.danji_power = tb_newdanjiPower2.Text;
            danjiModel.danji_type = cb_danjiType2.SelectedItem.ToString();
            danjiModel.danji_dianchangId = intDianchangId.ToString();
            dianchangModel.dianchang_capacity = (Convert.ToInt32(dianchangModel.dianchang_capacity) + Convert.ToInt32(tb_newdanjiPower2.Text)).ToString();
            PPE.AddTodanji(danjiModel);

            PPE.SaveChanges();
            cb_adddanji_dianchang_SelectedIndexChanged(null, null);
            DrawGridView();
            MessageBox.Show("机组添加成功！！");
        }
        private void button1_Click(object sender, EventArgs e)
        {
            DrawSummary();
        }
        #endregion

        private void tb_newdianchang_name_TextChanged(object sender, EventArgs e)
        {
            if (tb_newdianchang_name.Text.Length >= 11)
            {
                tb_newdianchang_name.Text = tb_newdianchang_name.Text.Substring(0, tb_newdianchang_name.Text.Length - 1);
                tb_newdianchang_name.SelectAll();
                tb_newdianchang_name.SelectionStart = tb_newdianchang_name.Text.Length;
                tb_newdianchang_name.Focus();
            }
            tb_newdanji_name.Text = tb_newdianchang_name.Text + "--" + "1";
        }
        private void dtp_fsgl_start_ValueChanged(object sender, EventArgs e)
        {
            ReDrawChart();
        }
        private void dtp_fsgl_end_ValueChanged(object sender, EventArgs e)
        {
            ReDrawChart();
        }
        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (tabControl1.SelectedIndex)
            {
                case 2: LoadSettingPage(); break;
                default: return;
            }
        }
        private void tabControl2_SelectedIndexChanged(object sender, EventArgs e)
        {
            DrawGridView();
        }
        private void tabControl3_SelectedIndexChanged(object sender, EventArgs e)
        {
            ReDrawChart();
        }
        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            ReDrawChart();
        }
        private void trackBar2_ValueChanged(object sender, EventArgs e)
        {
            ReDrawChart();
        }
        private void trackBar3_Scroll(object sender, EventArgs e)
        {
            ReDrawChart();
        }
        private void trackBar4_Scroll(object sender, EventArgs e)
        {
            ReDrawChart();
        }
        private void trackBar5_Scroll(object sender, EventArgs e)
        {
            ReDrawChart();
        }
        private void lv_newdianchang_danji_MouseUp(object sender, MouseEventArgs e)
        {
            lv_newdianchang_danji.ContextMenuStrip = cms_lvitemmenu;
        }
        private void cb_fsgl_dianchang_SelectedIndexChanged(object sender, EventArgs e)
        {
            PowerPlantEntities PPE = new PowerPlantEntities();
            string dianchangName = cb_fsgl_dianchang.SelectedItem.ToString();
            int dianchang_id = PPE.dianchang.FirstOrDefault(c => c.dianchang_name == dianchangName).dianchang_id;
            string temp = dianchang_id.ToString();
            List<danji> list = PPE.danji.Where(c => c.danji_dianchangId == temp).ToList();
            cb_fsgl_danji.Items.Clear();
            foreach (danji item in list)
            {
                cb_fsgl_danji.Items.Add(item.danji_name);
            }
            cb_fsgl_danji.SelectedItem = cb_fsgl_danji.Items[0];
        }
        private void cb_fsgl_danji_SelectedIndexChanged(object sender, EventArgs e)
        {
            ReDrawChart();
        }
        private void cb_fsgl_dianchang_Click(object sender, EventArgs e)
        {
            BindComboBoxItem(cb_fsgl_dianchang, "dianchang");
        }
        private void cb_manage_dianchang2_SelectedIndexChanged(object sender, EventArgs e)
        {
            PowerPlantEntities PPE = new PowerPlantEntities();
            string dianchangName = cb_manage_dianchang1.SelectedItem.ToString();
            int dianchang_id = PPE.dianchang.FirstOrDefault(c => c.dianchang_name == dianchangName).dianchang_id;
            string temp = dianchang_id.ToString();
            List<danji> list = PPE.danji.Where(c => c.danji_dianchangId == temp).ToList();
            cb_manage_danji.Items.Clear();
            foreach (danji item in list)
            {
                cb_manage_danji.Items.Add(item.danji_name);
            }
            cb_manage_danji.SelectedItem = cb_manage_danji.Items[0];
        }
        private void cb_manage_danji_SelectedIndexChanged(object sender, EventArgs e)
        {
            DrawGridView();
        }
        private void cb_manage_dianchang1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DrawGridView();
        }
        private void cb_import_dianchang_SelectedIndexChanged(object sender, EventArgs e)
        {
            PowerPlantEntities PPE = new PowerPlantEntities();
            string dianchangName = cb_import_dianchang.SelectedItem.ToString();
            int dianchang_id = PPE.dianchang.FirstOrDefault(c => c.dianchang_name == dianchangName).dianchang_id;
            string temp = dianchang_id.ToString();
            List<danji> list = PPE.danji.Where(c => c.danji_dianchangId == temp).ToList();
            cb_import_danji.Items.Clear();
            foreach (danji item in list)
            {
                cb_import_danji.Items.Add(item.danji_name);
            }
            cb_import_danji.SelectedItem = cb_import_danji.Items[0];
        }
        private void cb_adddanji_dianchang_SelectedIndexChanged(object sender, EventArgs e)
        {
            PowerPlantEntities PPE = new PowerPlantEntities();
            string dianchangName=cb_adddanji_dianchang.SelectedItem.ToString();
            var dianchangId = PPE.dianchang.FirstOrDefault(c => c.dianchang_name == dianchangName ).dianchang_id;
            string tempId=dianchangId.ToString();
            var danjiList = PPE.danji.Where(c=>c.danji_dianchangId==tempId);
            lv_danjiList2.Items.Clear();
            foreach (danji item in danjiList)
            {
                lv_danjiList2.Items.Add(item.danji_name);
            }
            string lastName=lv_danjiList2.Items[lv_danjiList2.Items.Count-1].Text;
            lastName=lastName.Substring(lastName.LastIndexOf("--")+2);
            tb_newdanji_name2.Text = cb_adddanji_dianchang.SelectedItem.ToString() + "--" +(Convert.ToInt32( lastName)+1).ToString();
        }
        private void cms_lvitemmenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Text == "删除")
            {
                lv_newdianchang_danji.Items.Remove(lv_newdianchang_danji.SelectedItems[0]);
            }
        }
    }
    public class ComboBoxModel
    {
        public string Text { get; set; }
        public string Value { get; set; }
        public override string ToString()
        {
            return Text;
        }
    }
    public enum ConfigurationList
    {
        danjiFengsuMax,
        danjiFengsuMin,
        danjiChuliMax,
        danjiChuliMin,
        fsglScroll,
        clglScroll,
        clqxScroll,
        fsqxScroll,
        statusInterval
    }
}
